<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="positionType">
        <el-select v-model="queryParams.positionType" placeholder="请选择类型" clearable style="width: 240px">
          <el-option
            v-for="dict in position_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="父类型" prop="parentId">
        <el-input
          v-model="queryParams.parentId"
          placeholder="请输入父类型"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="排序" prop="orderNum">
        <el-input
          v-model="queryParams.orderNum"
          placeholder="请输入排序"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择产品状态" clearable style="width: 240px">
          <el-option
            v-for="dict in status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['decor:position:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="Sort"
          @click="toggleExpandAll"
        >展开/折叠</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['decor:position:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="positionList"
      row-key="positionId"
      :default-expand-all="isExpandAll"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column label="名称" align="center" prop="name">
        <template #default="scope">
          <!-- <dict-tag :options="${column.dictType}" :value="scope.row.name"/> -->
        </template>
      </el-table-column>
      <el-table-column label="类型" align="center" prop="positionType">
        <template #default="scope">
          <dict-tag :options="position_type" :value="scope.row.positionType"/>
        </template>
      </el-table-column>
      <el-table-column label="父类型" align="center" prop="parentId">
        <template #default="scope">
          <!-- <dict-tag :options="${column.dictType}" :value="scope.row.parentId"/> -->
        </template>
      </el-table-column>
      <el-table-column label="排序" align="center" prop="orderNum">
        <template #default="scope">
          <!-- <dict-tag :options="${column.dictType}" :value="scope.row.orderNum"/> -->
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <!-- <el-table-column label="祖级列表" align="center" prop="ancestors">
        <template #default="scope">
          <dict-tag :options="${column.dictType}" :value="scope.row.ancestors"/>
        </template>
      </el-table-column> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['decor:position:edit']">修改</el-button>
          <el-button link type="primary" icon="Plus" @click="handleAdd(scope.row)" v-hasPermi="['decor:position:add']">新增</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['decor:position:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改职位职业对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="positionRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="类型" prop="positionType">
          <el-radio-group v-model="form.positionType">
            <el-radio
              v-for="dict in position_type"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="父类型" prop="parentId">
          <el-tree-select
            v-model="form.parentId"
            :data="positionOptions"
            :props="{ value: 'positionId', label: 'name', children: 'children' }"
            value-key="positionId"
            placeholder="请选择父类型"
            check-strictly
          />
        </el-form-item>
        <el-form-item label="排序" prop="orderNum">
          <el-input v-model="form.orderNum" placeholder="请输入排序" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in status"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { listPosition, getPosition, delPosition, addPosition, updatePosition } from "@/api/decor/position";

const { proxy } = getCurrentInstance();
const { status, position_type } = proxy.useDict('status', 'position_type');

// 树表表格数据
const positionList = ref([]);
// 树选项
const positionOptions = ref([]);
// 是否显示弹出层
const open = ref(false);
// 遮罩层
const loading = ref(true);
// 显示搜索条件
const showSearch = ref(true);
// 弹出层标题
const title = ref("");
// 是否展开，默认全部展开
const isExpandAll = ref(true);
// 重新渲染表格状态
const refreshTable = ref(true);

const data = reactive({
  form: {},
  queryParams: {
    name: null,
    positionType: null,
    parentId: null,
    status: null,
    ancestors: null
  },
  rules: {
    // positionId: [
    //   { required: true, message: "职位ID不能为空", trigger: "blur" }
    // ],
    // tenantId: [
    //   { required: true, message: "租户编号不能为空", trigger: "blur" }
    // ],
    name: [
      { required: true, message: "名称不能为空", trigger: "blur" }
    ],
    positionType: [
      { required: true, message: "类型", trigger: "change" }
    ],
    parentId: [
      { required: true, message: "父产品id不能为空", trigger: "blur" }
    ],
    status: [
      { required: true, message: "状态不能为空", trigger: "change" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询职位职业列表 */
function getList() {
  loading.value = true;
  listPosition(queryParams.value).then(response => {
    positionList.value = proxy.handleTree(response.data, "positionId", "parentId");
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    positionId: null,
    tenantId: null,
    name: null,
    positionType: null,
    parentId: null,
    status: null,
    delFlag: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    ancestors: null
  };
  proxy.resetForm("positionRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 新增按钮操作 */
async function handleAdd(row) {
  reset();
  await listPosition().then(response => {
      positionOptions.value = proxy.handleTree(response.data, "positionId");
  });
  if (row != null && row.positionId) {
    form.value.parentId = row.positionId;
  } else {
    form.value.parentId = 0;
  }
  open.value = true;
  title.value = "添加职位职业";
}

/** 展开/折叠操作 */
function toggleExpandAll() {
  refreshTable.value = false;
  isExpandAll.value = !isExpandAll.value;
  nextTick(() => {
    refreshTable.value = true;
  });
}

/** 修改按钮操作 */
async function handleUpdate(row) {
  reset();
  await listPosition().then(response => {
      positionOptions.value = proxy.handleTree(response.data, "positionId");
  });
  if (row != null) {
    form.value.parentId = row.positionId;
  }
  getPosition(row.positionId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改职位职业";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["positionRef"].validate(valid => {
    if (valid) {
      if (form.value.positionId != null) {
        updatePosition(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addPosition(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }else{
            proxy.$modal.msgError('请根据提示完善必填信息')
        }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  proxy.$modal.confirm('是否确认删除职位职业编号为"' + row.positionId + '"的数据项？').then(function() {
    return delPosition(row.positionId);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
    proxy.download('decor/position/export', {
        ...queryParams.value
    }, `position_${new Date().getTime()}.xlsx`)
}

getList();
</script>
